using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Products
    {
         #region Common
        public eProducts   Data;

        public Products()
        {
            Data = new eProducts();
		}

        ~Products()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cProducts objDAL = new cProducts();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cProducts objDAL = new cProducts();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }
        public DataTable GetAll()
        {
            DataTable tbResult = new DataTable();
            cProducts objDAL = new cProducts();
            tbResult = objDAL.GetAll();
            return tbResult;
        }
        public int Insert()
        {
            int nResult = 0;
            cProducts objInfor = new cProducts(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.ProductID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cProducts objInfor = new cProducts();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }
        public int UpdatePriority(int ProductID, int Priority)
        {
            int nResult = 0;
            cProducts objDAL = new cProducts();
            try
            {
                objDAL.LoadByPrimaryKey(ProductID);
                objDAL.Priority = Priority;

                objDAL.Save();
                nResult = objDAL.ProductID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cProducts objDAL = new cProducts();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
                objDAL.Save();
                nResult = objDAL.ProductID ;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cProducts objInfor)
        {
            objInfor.ProductCode = Data.ProductCode;
            objInfor.ProductCategoryID = Data.ProductCategoryID;
            objInfor.TradeMarkID = Data.TradeMarkID;

            try { objInfor.MaterialID = Data.MaterialID; }
            catch { objInfor.MaterialID = 0; }

            try { objInfor.ImageDefault = Data.ImageDefault; }
            catch { objInfor.ImageDefault = ""; } 

            try { objInfor.Image1 = Data.Image1;}
            catch { objInfor.Image1 = ""; }

            try {objInfor.Image2 = Data.Image2;}
            catch { objInfor.Image2 = ""; }

            try {objInfor.Image3 = Data.Image3;}
            catch { objInfor.Image3 = ""; }

            try { objInfor.Image4 = Data.Image4; }
            catch { objInfor.Image4 = ""; }

            try { objInfor.MainContent1 = Data.MainContent1; }
            catch { objInfor.MainContent1 = ""; }

            try { objInfor.MainContent2 = Data.MainContent2; }
            catch
            {
                objInfor.MainContent2 = "";
            }
            try { objInfor.MainContent3 = Data.MainContent3; }
            catch { objInfor.MainContent3 = ""; }
            try { objInfor.Price = Data.Price; }
            catch { objInfor.Price = 0; }
            try { objInfor.PriceMarket = Data.PriceMarket; }
            catch { objInfor.PriceMarket = 0; }
            try { objInfor.Colors = Data.Colors; }
            catch { objInfor.Colors = ""; }
            objInfor.Priority = Data.Priority;
            try { objInfor.Size = Data.Size; }
            catch { objInfor.Size = ""; }
            objInfor.CreatedDate = Data.CreatedDate;
            objInfor.IsSalesOff = Data.IsSalesOff;
            try { objInfor.Tag = Data.Tag; }
            catch { objInfor.Tag = ""; }
            try { objInfor.SalesOffDescription = Data.SalesOffDescription; }
            catch
            {
                objInfor.SalesOffDescription = "";
            
            }
            objInfor.Status = Data.Status;
            try { objInfor.ProductName1 = Data.ProductName1; }
            catch { objInfor.ProductName1 = ""; }

            try {objInfor.ProductName2 = Data.ProductName2;}
            catch { objInfor.ProductName2 = ""; }

            try {objInfor.ProductName3 = Data.ProductName3;}
            catch { objInfor.ProductName3 = ""; }

            try {objInfor.PriceSaleOff = Data.PriceSaleOff;}
            catch { objInfor.PriceSaleOff = 0; }

            try { objInfor.PercentSaleOff = Data.PercentSaleOff; }
            catch { objInfor.PriceSaleOff = 0; }

            try { objInfor.ExpriedDateSaleOff = Data.ExpriedDateSaleOff; }
            catch { objInfor.ExpriedDateSaleOff = 0; }
            try { objInfor.StartedDateSaleOff = Data.StartedDateSaleOff; }
            catch { objInfor.StartedDateSaleOff = 0; }

            try { objInfor.Original = Data.Original; }
            catch { objInfor.Original = "" ; }
            try { objInfor.Specifications1 = Data.Specifications1; }
            catch { objInfor.Specifications1 =""; }

            try { objInfor.Specifications2 = Data.Specifications2; }
            catch { objInfor.Specifications2 = ""; }
            try { objInfor.Specifications3 = Data.Specifications3; }
            catch { objInfor.Specifications3 = ""; }
            objInfor.BestSeller = Data.BestSeller;

            try { objInfor.UserGuide = Data.UserGuide; }
            catch { objInfor.UserGuide = ""; }
        }

        private void ConvertData(cProducts objInfor)
        {
            Data.ProductCode =objInfor.ProductCode  ;
            Data.ProductCategoryID =objInfor.ProductCategoryID  ;
            Data.TradeMarkID=objInfor.TradeMarkID  ;
            Data.MaterialID  =objInfor.MaterialID  ;
            Data.ImageDefault  =objInfor.ImageDefault  ;
            Data.Image1 =objInfor.Image1 ;
            Data.Image2=objInfor.Image2 ;
            Data.Image3 =objInfor.Image3 ;
            Data.Image4 = objInfor.Image4;
            Data.MainContent1 =objInfor.MainContent1  ;
            Data.MainContent2 = objInfor.MainContent2;
            Data.MainContent3 = objInfor.MainContent3;
            Data.Price  =objInfor.Price ;
            Data.PriceMarket  =objInfor.PriceMarket ;
            Data.Colors =objInfor.Colors  ;
            Data.Priority =objInfor.Priority  ;
            Data.Size=objInfor.Size  ;
            Data.CreatedDate =objInfor.CreatedDate  ;
            Data.IsSalesOff =objInfor.IsSalesOff ;
            Data.SalesOffDescription =objInfor.SalesOffDescription  ;
            Data.Status =objInfor.Status  ;
           Data.ProductName1 =objInfor.ProductName1  ;
           Data.ProductName2 = objInfor.ProductName2;
           Data.ProductName3 = objInfor.ProductName3;
           Data.PriceSaleOff =objInfor.PriceSaleOff  ;
           Data.PercentSaleOff=objInfor.PercentSaleOff  ;
           Data.ExpriedDateSaleOff = objInfor.ExpriedDateSaleOff;
           Data.StartedDateSaleOff = objInfor.StartedDateSaleOff;
           Data.Tag = objInfor.Tag;
           Data.Specifications1 = objInfor.Specifications1;
           Data.Specifications2 = objInfor.Specifications2;
           Data.Specifications3 = objInfor.Specifications3;
           Data.Original = objInfor.Original;
           Data.BestSeller = objInfor.BestSeller;
           Data.UserGuide = objInfor.UserGuide;
        }
        #endregion

        public DataTable Search(int ItemView,string Inclause, string Title, int ItemID, string ProductCategoryID,
                                int TradeMarkID, int MaterialID, double PriceFrom, double PriceTo, int isSalesOff,int BestSeller,
                                int Status, int PercentSaleOff,
                                int ExpriedDateSaleOff, int StartedDateSaleOff, int DefaultLangId, int CurrentLangId, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cProducts objDAL = new cProducts();
            tbResult = objDAL.Search(ItemView,Inclause, Title, ItemID, ProductCategoryID,TradeMarkID, MaterialID,PriceFrom, PriceTo, isSalesOff,BestSeller,
                                 Status, PercentSaleOff,ExpriedDateSaleOff, StartedDateSaleOff, DefaultLangId,  CurrentLangId,PageIndex,PageSize,ref PageCount);
            return tbResult;
        } 
    }
}
